package common

import (
	"errors"
	"github.com/gin-gonic/gin"
	"wx-uniapp-go/internal/enums"
	"wx-uniapp-go/internal/server/echo"
	"wx-uniapp-go/third_party/cdslog"
	"wx-uniapp-go/third_party/oss"
)

// UploadOss 上传文件到OSS
func UploadOss(c *gin.Context) {

	file, err := c.FormFile("file")
	if err != nil {
		echo.Fail(c).Error(err)
		return
	}
	if file.Size > 2<<30 {
		echo.DirectBadRequest(c, "上传文件不能大于2G")
		return
	}
	source := c.Request.FormValue("source")
	if source == "" {
		echo.Fail(c).Error(errors.New("资源来源类型不能为空"))
		return
	}

	prefix := ""
	switch source {
	case "activity":
		prefix = enums.UploadActivity
	case "avatar":
		prefix = enums.UploadAvatar
	default:
		prefix = "/res/upload/other"
	}
	url, err := oss.Cli.UploadFile(file, true, prefix)
	if err != nil {
		cdslog.Error(err.Error())
		echo.DirectBadRequest(c, "上传失败")
		return
	}
	echo.Success(c, url)
}
